import { getUsersGetCurrentUserInfo } from '@/services/bankend/userController';
import { Card, Descriptions, message, Spin } from 'antd';
import React, { useEffect, useState } from 'react';

const Profile: React.FC = () => {
  const [user, setUser] = useState<API.Users | null>(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    getUsersGetCurrentUserInfo()
      .then((res) => {
        if (res.code === 0 && res.data) {
          setUser(res.data);
        } else {
          message.error(res.message || '获取用户信息失败');
        }
      })
      .catch(() => {
        message.error('获取用户信息失败');
      })
      .finally(() => setLoading(false));
  }, []);

  return (
    <div style={{ maxWidth: 600, margin: '40px auto', padding: 16 }}>
      <Card title="个人信息" bordered>
        {loading ? (
          <Spin />
        ) : user ? (
          <Descriptions column={1}>
            <Descriptions.Item label="姓名">
              {user.name || '-'}
            </Descriptions.Item>
            <Descriptions.Item label="学号/用户名">
              {user.username || '-'}
            </Descriptions.Item>
            <Descriptions.Item label="角色">
              {user.role || '-'}
            </Descriptions.Item>
            <Descriptions.Item label="学历">
              {user.degree || '-'}
            </Descriptions.Item>
            <Descriptions.Item label="邮箱">
              {user.email || '-'}
            </Descriptions.Item>
            <Descriptions.Item label="手机号">
              {user.phone || '-'}
            </Descriptions.Item>
            <Descriptions.Item label="创建时间">
              {user.createTime || '-'}
            </Descriptions.Item>
            <Descriptions.Item label="更新时间">
              {user.updateTime || '-'}
            </Descriptions.Item>
          </Descriptions>
        ) : (
          <div style={{ color: '#f5222d' }}>未获取到用户信息</div>
        )}
      </Card>
    </div>
  );
};

export default Profile;
